from javax.swing import *
from java.awt import FlowLayout, GridLayout, BorderLayout
from java.awt.event import KeyEvent
import sys

class TreinoLivre:

    def __init__(self, param1, param2):
        self.frame = JFrame("Calculo de treino livre (pratique)",\
            layout=BorderLayout(), \
            defaultCloseOperation=WindowConstants.EXIT_ON_CLOSE)
            
        self.fields = [['Aero D.:', param1, (1, param1/2, param1), 1],
            ['Aero T.:', param1, (1, param1/2, param1), 1],
            ['Acel R.:', param1, (1, param1/2, param1), 1],
            ['Acel C.:', param1, (1, param1/2, param1), 1],
            ['Brake :', param1, (1, param1/2, param1), 1],
            ['Volante:', param2, (1, param2/2, param1), 1]]

        self.panel = JPanel(layout=GridLayout(6,5))
        for i,field in enumerate(self.fields):

            text, value = field[0], field[2]

            self.panel.add(JLabel(text))
            tf = JTextField(str(value), enabled=False)
            self.panel.add(tf)
            field.append(tf)

            self.panel.add(JButton('+', actionPerformed=\
                lambda evt, f=field, tf=tf: self.inc(evt, f)))
            self.panel.add(JButton('-', actionPerformed=\
                lambda evt, f=field, tf=tf: self.dec(evt, f)))
            self.panel.add(JButton('reset', actionPerformed=\
                lambda evt, f=field, tf=tf: self.reset(evt, f)))
        self.frame.add(self.panel, BorderLayout.NORTH)

        input_label = JLabel('Input: ')
        self.input_text = JTextField('', keyPressed=self.keyPressed)
        p = JPanel(layout=GridLayout(1,2))
        p.add(input_label)
        p.add(self.input_text)
        self.frame.add(p, BorderLayout.SOUTH)

        self.frame.pack()
        self.input_text.requestFocus()
        self.input_text.setToolTipText('Caracteres validos: + e -.'\
            'Demais sao ignorados.')

    def keyPressed(self, evt):
        if evt.keyCode != KeyEvent.VK_ENTER:
            return
        for (cmd, field) in zip(self.input_text.getText(), self.fields):
            if cmd == '+':
                self.inc(None, field)
            elif cmd == '-':
                self.dec(None, field)
        self.input_text.setSelectionStart(0)
        self.input_text.setSelectionEnd(len(self.input_text.getText()))

    def inc(self, event, data):
        initial_value, (lo, value, hi), iteration, textField = data[1:]
        iteration += 1
        lo = value
        value += (hi - value) / 2
        data[2:4] = [(lo, value, hi), iteration]
        print data[:-1]
        textField.setText(str(data[2]))

    def dec(self, event, data):
        initial_value, (lo, value, hi), iteration, textField = data[1:]
        iteration += 1
        hi = value
        value -= (value - lo) / 2
        data[2:4] = [(lo, value, hi), iteration]
        print data[:-1]
        textField.setText(str(data[2]))

    def reset(self, event, data):
        initial_value, (lo, value, hi), iteration, textField = data[1:]
        data[2] = (1, initial_value / 2, initial_value)
        data[3] = 1
        print data[:-1]
        textField.setText(str(data[2]))
        
    def show(self):
        self.frame.show()

if __name__ == '__main__':
    UIManager.setLookAndFeel('com.sun.java.swing.plaf.gtk.GTKLookAndFeel')
    if len(sys.argv) < 3:
        print '\n%s param1 param2\n' % sys.argv[0]
        sys.exit(1)
    t = TreinoLivre(int(sys.argv[1]), int(sys.argv[2]))
    t.show()

